package leetcode.code1302;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.helper.tree.TreeNode;

public class Solution {
	public int deepestLeavesSum(TreeNode root) {
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);
		int ans = 0;
		while (!queue.isEmpty()) {
			int size = queue.size();
			ans = 0;
			while (size > 0) {
				TreeNode cur = queue.poll();
				ans += cur.val;
				if (cur.left != null) {
					queue.add(cur.left);
				}
				if (cur.right != null) {
					queue.add(cur.right);
				}
				size--;
			}
		}
		return ans;
	}
}
